<template>
  <div class="page-container">
    <!-- 左侧栏 -->
    <div class="left-box">
      <div class="search-box">
        <input type="text" placeholder="片区名称" class="search-input">
        <i class="search-icon">🔍</i>
      </div>
      
      <div class="nav-list">
        <div class="nav-item" @click="toggleSubItems">
          <span class="nav-text">宁波市</span>
          <span class="arrow">{{ showSubItems ? '▲' : '▼' }}</span>
        </div>
        <div v-if="showSubItems" class="nav-sub-item">
          <span class="nav-text">海曙区</span>
        </div>
        <div v-if="showSubItems" class="nav-sub-item">
          <span class="nav-text">北仑区</span>
        </div>
        <div v-if="showSubItems" class="nav-sub-item">
          <span class="nav-text">象山县</span>
        </div>
        <div v-if="showSubItems" class="nav-sub-item">
          <span class="nav-text">宁海县</span>
        </div>
        <div v-if="showSubItems" class="nav-sub-item">
          <span class="nav-text">慈溪市</span>
        </div>
      </div>
    </div>
    <!-- 右侧区域 -->
    <div class="right-section">
      <!-- 右上盒子 -->
      <div class="right-top-box">
        <h2>任务统计</h2>
        <div class="stats-container">
          <div class="stat-item yellow">
            <p>片区数量</p>
            <span>36</span>
          </div>
          <div class="stat-item red">
            <p>任务完成率</p>
            <span>25.59%</span>
          </div>
          <div class="stat-item blue">
            <p>任务总数</p>
            <span>340</span>
          </div>
          <div class="stat-item white">
            <div class="stat-sub">
              <p>常态化任务：</p>
              <span>327.00</span>
            </div>
            <div class="stat-sub">
              <p>阶段性任务：</p>
              <span>13.00</span>
            </div>
          </div>
          <div class="stat-item purple">
            <p>上报事件</p>
            <span>316</span>
          </div>
        </div>
      </div>
      <!-- 右下盒子 -->
      <div class="right-bottom-box">
        <div class="table-container">
          <table class="custom-table">
            <thead>
              <tr>
                <th>序号</th>
                <th>片区名称</th>
                <th>任务总数</th>
                <th>已完成</th>
                <th>未完成</th>
                <th>完成率</th>
                <th>操作</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="(row, index) in currentPageData" :key="index">
                <td>{{ (currentPage - 1) * pageSize + index + 1 }}</td>
                <td>{{ row.name }}</td>
                <td>{{ row.total }}</td>
                <td>{{ row.completed }}</td>
                <td>{{ row.uncompleted }}</td>
                <td>{{ row.rate }}</td>
                <td>
                  <button class="action-btn" @click="showDialog(row)">查看</button>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
        <div class="pagination">
          <span class="total-info">合计: {{ tableData.length }}条</span>
          <div class="pagination-controls">
            <button 
              :disabled="currentPage === 1" 
              @click="currentPage--" 
              class="page-btn"
            >
              &lt;
            </button>
            <span class="page-number" 
              v-for="page in displayPages" 
              :key="page"
              :class="{ active: currentPage === page }"
              @click="currentPage = page"
            >
              {{ page }}
            </span>
            <button 
              :disabled="currentPage === totalPages" 
              @click="currentPage++" 
              class="page-btn"
            >
              &gt;
            </button>
          </div>
        </div>
      </div>
    </div>

    <!-- 弹窗组件 -->
    <div class="dialog-overlay" v-if="isDialogVisible" @click="closeDialog">
      <div class="dialog-content" @click.stop>
        <div class="dialog-header">
          <h3>任务详情</h3>
          <button class="close-btn" @click="closeDialog">×</button>
        </div>
        <div class="dialog-body">
          <div class="form-group">
            <label>片区名称：</label>
            <input type="text" v-model="formData.name" readonly>
          </div>
          <div class="form-group">
            <label>任务总数：</label>
            <input type="text" v-model="formData.total" readonly>
          </div>
          <div class="form-group">
            <label>已完成：</label>
            <input type="text" v-model="formData.completed" readonly>
          </div>
          <div class="form-group">
            <label>未完成：</label>
            <input type="text" v-model="formData.uncompleted" readonly>
          </div>
          <div class="form-group">
            <label>完成率：</label>
            <input type="text" v-model="formData.rate" readonly>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'

const currentPage = ref(1)
const pageSize = ref(5) // 每页显示5条数据
const showSubItems = ref(true) // 控制子项显示的状态

const tableData = ref([
  { name: '段塘街道丽园片区', total: 42, completed: 12, uncompleted: 30, rate: '28.57%' },
  { name: '段塘街道启运片区', total: 21, completed: 0, uncompleted: 21, rate: '0.00%' },
  { name: '江厦街道新街片区', total: 12, completed: 0, uncompleted: 12, rate: '0.00%' },
  { name: '江厦街道天一商圈片区', total: 8, completed: 0, uncompleted: 8, rate: '0.00%' },
  { name: '江厦街道青安里片区', total: 8, completed: 0, uncompleted: 8, rate: '0.00%' },
  { name: '江厦街道西共美工会发展片区', total: 8, completed: 0, uncompleted: 8, rate: '0.00%' },
  { name: '江厦街道中核心商圈片区', total: 8, completed: 0, uncompleted: 8, rate: '0.00%' },
  { name: '江厦街道天一商圈片区', total: 8, completed: 0, uncompleted: 8, rate: '0.00%' },
  { name: '江厦街道青安里片区', total: 8, completed: 0, uncompleted: 8, rate: '0.00%' },
  { name: '江厦街道西共美工会发展片区', total: 8, completed: 0, uncompleted: 8, rate: '0.00%' },
  // 添加更多数据...
])

const totalPages = computed(() => Math.ceil(tableData.value.length / pageSize.value))

const currentPageData = computed(() => {
  const start = (currentPage.value - 1) * pageSize.value
  const end = start + pageSize.value
  return tableData.value.slice(start, end)
})

const displayPages = computed(() => {
  const pages = []
  const maxDisplayPages = 5
  let start = Math.max(1, currentPage.value - 2)
  let end = Math.min(totalPages.value, start + maxDisplayPages - 1)
  
  if (end - start + 1 < maxDisplayPages) {
    start = Math.max(1, end - maxDisplayPages + 1)
  }
  
  for (let i = start; i <= end; i++) {
    pages.push(i)
  }
  return pages
})

const isDialogVisible = ref(false)
const formData = ref({
  name: '',
  total: '',
  completed: '',
  uncompleted: '',
  rate: ''
})

const showDialog = (row) => {
  formData.value = { ...row }
  isDialogVisible.value = true
}

const closeDialog = () => {
  isDialogVisible.value = false
}

const toggleSubItems = () => {
  showSubItems.value = !showSubItems.value; // 切换子项显示状态
}
</script>

<style scoped>
.page-container {
  display: flex;
  width: 100%;
  height: 100vh;
  gap: 20px;
  padding: 20px;
  background-color: #f9f9f9; /* 背景颜色 */
}

.left-box {
  width: 300px;
  background-color: #fff;
  border-radius: 8px;
  border: 1px solid #e0e0e0;
  padding: 16px;
  height: 100%;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); /* 添加阴影 */
}

.search-box {
  position: relative;
  margin-bottom: 16px;
}

.search-input {
  width: 90%;
  padding: 8px 32px 8px 12px;
  border: 1px solid #e0e0e0;
  border-radius: 4px;
  font-size: 14px;
}

.search-icon {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: #999;
  cursor: pointer;
}

.nav-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.nav-item, .nav-sub-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 12px;
  cursor: pointer;
  border-radius: 4px;
}

.nav-item:hover, .nav-sub-item:hover {
  background-color: #f5f5f5;
}

.nav-item {
  font-weight: 500;
}

.nav-sub-item {
  padding-left: 24px;
  color: #666;
}

.arrow {
  font-size: 12px;
  color: #999;
}

.right-section {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.right-top-box {
  height: 210px;
  background-color: #ffffff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  padding: 16px; /* 添加内边距 */
}

.right-bottom-box {
  flex: 1;
  background-color: #f0f0f0;
  border-radius: 8px;
  display: grid;
  margin-top: 10px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); /* 添加阴影 */
}

.stats-container {
  display: flex;
  justify-content: space-between; /* 使小卡片在同一行 */
  gap: 5px; /* 减小间距 */
  padding: 5px; /* 添加内边距 */
  height: 100%; /* 确保容器高度适应 */
}

.stat-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 15px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  text-align: center;
}

.stat-item p {
  flex:1;
  margin: 2px;
  padding-top: 4px;
  color: #666;
  font-size: 16px;
  strong {
    color: #333;
  }
}

.stat-item span {
  flex:1;
  margin-top: 2px;
  font-size: 16px;
  font-weight: bold;
  color: #333;
}

.yellow { background-color: #e3ce8b;height: 100px;padding-top:2px;}
.red { background-color: #dd948f; height: 100px;padding-top:2px;}
.blue { background-color: #82c8e9; height: 100px;padding-top:2px;}
.white { background-color: #dce792;height: 100px; padding-top:2px;}
.purple { background-color: #8b81e5;height: 100px; padding-top:2px;}

.table-container {
  flex: 0 0 auto;
  display: flex;
  justify-content: flex-end;
  align-items: flex-end;
  width: 100%;
}

.custom-table {
  width: 100%;
  border-collapse: collapse;
  background-color: #fff;
  border-radius: 8px;
  overflow: hidden;
}

.custom-table th,
.custom-table td {
  padding: 12px 15px;
  text-align: center;
  border-bottom: 1px solid #e8e8e8;
}

.custom-table th {
  background-color: #fafafa;
  font-weight: 500;
  color: #333;
}

.custom-table tr:hover {
  background-color: #f5f5f5; /* 鼠标悬停效果 */
}

.action-btn {
  padding: 4px 15px;
  background-color: #1890ff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.action-btn:hover {
  background-color: #40a9ff;
}

.pagination {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 20px;
  background-color: #fff;
  border-top: 1px solid #e8e8e8;
}

.pagination-controls {
  display: flex;
  align-items: center;
  gap: 8px;
}

.page-btn {
  padding: 4px 2px;
  border: 1px solid #d9d9d9;
  background-color: #fff;
  border-radius: 4px;
  cursor: pointer;
  color: #666;
}

.page-btn:disabled {
  cursor: not-allowed;
  color: #d9d9d9;
}

.page-number {
  padding: 4px 12px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  cursor: pointer;
  color: #666;
}

.page-number.active {
  background-color: #1890ff;
  color: #fff;
  border-color: #1890ff;
}

.page-info {
  margin-left: 15px;
  color: #666;
}

.total-info {
  color: #666;
}

/* 弹窗样式 */
.dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.dialog-content {
  background-color: #fff;
  border-radius: 8px;
  width: 500px;
  max-width: 90%;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
}

.dialog-header {
  padding: 16px 20px;
  border-bottom: 1px solid #e8e8e8;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.dialog-header h3 {
  margin: 0;
  font-size: 16px;
  color: #333;
}

.close-btn {
  border: none;
  background: none;
  font-size: 20px;
  color: #999;
  cursor: pointer;
  padding: 0;
}

.close-btn:hover {
  color: #666;
}

.dialog-body {
  padding: 20px;
}

.form-group {
  margin-bottom: 15px;
}

table {
  width: 100%; /* 表格宽度为100% */
  height: 100%; /* 表格高度为100% */
  border-collapse: collapse; /* 合并边框 */
}

.form-group label {
  display: block;
  margin-bottom: 5px;
  color: #666;
  font-size: 14px;
}

.form-group input {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid #e8e8e8;
  border-radius: 4px;
  font-size: 14px;
  color: #333;
  background-color: #f9f9f9;
}
</style>